#include <SFML/Graphics.hpp>
#include <string>
#include <sstream>
#include <iostream>
#include <fstream>
#include <cctype>
#include <cmath>
#include <ctime>
#include <vector>
#include "../include/Game.hpp"
#include "../include/GUI/GUIManager.hpp"
#include "../include/luaManager.hpp"
#include "../include/InputEngine.hpp"
//#include "Sprite.hpp"
//#include "Text.hpp"

sf::RenderWindow Window;
sf::View view;          // default viewport
sf::Clock gameClock;
sf::Vector2i screenDimensions; // resolution
sf::Font font;
// timers
float dt;
float frameCounter;
float frameSpeed;
float moveSpeed;
float switchFrame;

//const int maxPartySize = 12;

Game::Game() : gamePaused(false)
{
	sf::VideoMode desktop = sf::VideoMode::getDesktopMode();
	frameCounter = 0;
	frameSpeed = 600;
	switchFrame = 100;
	moveSpeed = 150;
	screenDimensions = sf::Vector2i(1280,720);
	Window.create(sf::VideoMode(screenDimensions.x, screenDimensions.y, desktop.bitsPerPixel), "Open Force");
	Window.setFramerateLimit(60);
	Window.setMouseCursorVisible(false);
	Window.setVerticalSyncEnabled(true);
	view.reset(sf::FloatRect(0,0,screenDimensions.x, screenDimensions.y));
	view.setViewport(sf::FloatRect(0,0, 1.0f, 1.0f));
	if (!font.loadFromFile("./data/Fonts/DejaVuSerif-Bold.ttf"))
		std::cout << "Cannot find font data.\n";
	std::cout << "Loading Game\n";
	// Initialize script engine
	LuaManager::Instance();
	// Initialize Global State
	GlobalState::Instance();
	// Bind Lua Definitions
	LuaBindings::BindDefinitionCode();
	// Set up input engine
	OForce_Input::InputEngine::Instance();
	// Initialize State Machine
//	mStateMachine = new OForce_Mode::StateMachine(&Window);
//	mStateMachine = new OForce_Mode::StateMachine();
	OForce_Mode::StateMachine::Instance();
	srand((unsigned)time(0));
}
Game::~Game()
{
//	delete mStateMachine;
	OForce_Mode::StateMachine::KillInstance();
	GlobalState::KillInstance();
	LuaManager::KillInstance();
}

void Game::play()
{
  bool fullScreen = false;
  sf::VideoMode desktop = sf::VideoMode::getDesktopMode();
  // Game Loop
  while(Window.isOpen()) {
	Window.clear();
	sf::Event Event;
	// move this into the keyPress function
	while(Window.pollEvent(Event)) {
		if (Event.type == sf::Event::Closed || Event.key.code == sf::Keyboard::Escape)
			Window.close();
		OForce_Input::InputEngine::Instance()->EventHandler(Event);
		if (Event.type == sf::Event::KeyPressed) {
			if (Event.key.code == sf::Keyboard::Space) {
				gamePaused = !gamePaused;
			} else if (Event.key.code == sf::Keyboard::F5) {
				fullScreen = !fullScreen;
				if (fullScreen) {
					Window.create(sf::VideoMode(screenDimensions.x, screenDimensions.y, desktop.bitsPerPixel), "Open Force", sf::Style::Fullscreen);
				} else {
					Window.create(sf::VideoMode(screenDimensions.x, screenDimensions.y, desktop.bitsPerPixel), "Open Force");
				}
			}
		}
	} // Window.pollEvent
//	if (gamePaused) {
//		sf::Vector2f viewCenter = view.getCenter();
//		pauseText.setPosition(viewCenter.x, viewCenter.y);
//		Window.draw(pauseText);
//	}
	OForce_Mode::StateMachine::Instance()->update();
	OForce_Mode::StateMachine::Instance()->draw();
	Window.setView(Window.getDefaultView());
	Window.display();
  }
}

